package util;

import lombok.Setter;

import java.util.List;

/**
 * @author tg.si@188.com
 * @todo
 * @since 2025/3/26 14:20
 */
@Setter
public class Condition {
    public static String toWhereClause(List<Condition>conditionList) {
        StringBuilder sb = new StringBuilder(" WHERE ");
        for (Condition c : conditionList) {
            sb.append(c.getKey()).append(" ").append(c.getOperator()).append(" ");
            String value = c.getValue();
            if("like".equalsIgnoreCase(c.getOperator())) {
                value = "%"+value+"%";
                value= TxtHelper.getSingleQuoteMarked(value);
            }
            sb.append(value);
            sb.append(" AND ");
        }
        TxtHelper.truncEnding(sb, 5);
        return sb.toString();
    }

    public Condition(String key, String operator, String value) {
        this.key = key;
        this.operator = operator;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getOperator() {
        return operator;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Condition{" +
                "key='" + key + "', " +
                "operator='" + operator + "', " +
                "value='" + value + "'" +
                '}';
    }

    // 字段名
    private String key;
    // 操作符
    private String operator;
    // 字段值
    private String value;
}